<?php
declare(strict_types=1);

namespace App\Service;

use App\Constants\StrStatus;
use App\Model\Dao\MchInfoDao;
use App\Model\Dao\SalesTransMchRelationDao;
use App\Model\Dao\SysAdminDao;
use App\Model\Dao\SysRoleDao;
use App\Model\Enum\CommissionHandleStatusEnum;
use App\Utils\CustomUtils;
use Hyperf\Di\Annotation\Inject;
use App\Model\Dao\CommissionSalesDao;
use App\Exception\XZXValidationException;
use App\Constants\XZXResponseCode;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use App\Utils\DateUtils;


class CommissionSalesService
{
    /**
     * @Inject()
     * @var CommissionSalesDao
     */
    private $CommissionSalesDao;

    /**
     * @Inject()
     * @var SysRoleDao
     */
    private $SysRoleDao;

    /**
     * @Inject()
     * @var SysAdminDao
     */
    private $SysAdminDao;

    /**
     * @Inject()
     * @var MchInfoDao
     */
    private $MchInfoDao;

    /**
     * @Inject()
     * @var SalesTransMchRelationDao
     */
    private $SalesTransMchRelationDao;

    public function get($param)
    {
        $result = $this->CommissionSalesDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        (isset($result['createTime']) && !empty($result['createTime'])) && $result['createTime'] = DateUtils::getDateTime($result['createTime']);

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function list(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];        

        $total = $this->CommissionSalesDao->getCount($param);
        $list = $this->CommissionSalesDao->getList($param, $page, $limit);

        foreach ($list as $k => $v) {
            (isset($v['createTime']) && !empty($v['createTime'])) && $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            $v['statYear'] = substr($v['statMonth'], 0, 4);
            $v['statMonth'] = substr($v['statMonth'], 5);
            $v['strTransStatus'] = CommissionHandleStatusEnum::getLabel($v['transStatus']);
            $list[$k] = $v;
        }

        $data = array(
            'total' => $total,
            'list' => $list
        );

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }

    public function getBindedMch($param) {
        $admin = $this->SysAdminDao->get($param['uid']);
        $role = $this->SysRoleDao->get($admin['roleId']);
        if ($role['extraType'] != 1) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::IS_NOT_SALES));
        }
        $relateAdmin = $this->SalesTransMchRelationDao->getWhere(['adminId' => $param['uid']]);
        $mchEmail = '';
        if (!empty($relateAdmin)) {
            $mch = $this->MchInfoDao->get($relateAdmin['mchId']);
            $mchEmail = $mch['email'];
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), $mchEmail);
    }

    public function bindMch($param) {
        $result = $this->checkMchEmailBinded($param);
        if ($result['binded']) {
            $updateRelate = [
                'mchId'         => $result['mch']['id'],
                'mchNo'         => $result['mch']['mchNo'],
            ];
            $this->SalesTransMchRelationDao->update($result['relate']['id'], $updateRelate);
        } else {
            $relate = [
                'id'            => CustomUtils::createUUID(),
                'adminId'       => $param['uid'],
                'mchId'         => $result['mch']['id'],
                'mchNo'         => $result['mch']['mchNo'],
                'status'        => 1,
                'createTime'    => time(),
            ];
            $this->SalesTransMchRelationDao->add($relate);
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), CustomUtils::xzxEmptyObject());
    }

    public function checkMchEmailBinded($param) {
        $isRelateAdmin = $this->SalesTransMchRelationDao->getWhere(['adminId' => $param['uid']]);

        $admin = $this->SysAdminDao->get($param['uid']);
        $role = $this->SysRoleDao->get($admin['roleId']);
        if ($role['extraType'] != 1) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::IS_NOT_SALES));
        }

        // 找不到商户
        $mch = $this->MchInfoDao->getWhere(['email' => $param['mchEmail']]);
        if (!$mch) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        $isRelateMchEmail = $this->SalesTransMchRelationDao->getWhere(['mchNo' => $mch['mchNo']]);
        // 新修改的被别人关联了
        if (!empty($isRelateMchEmail) && !empty($isRelateMchAdmin) && $isRelateMchEmail['mchNo'] != $isRelateAdmin['mchNo']) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::OCCUPIED_EMAIL));
        }

        return [
            'binded'    => !empty($isRelateAdmin),
            'mch'       => $mch,
            'relate'    => $isRelateAdmin
        ];
    }

}